package com.fivefu.core.camera.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.controller.BaseController;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.camera.entity.DbMonitorWorkorder;
import com.fivefu.core.camera.services.DbMonitorWorkorderService;
import com.fivefu.core.camera.services.SceneEventService;
import com.fivefu.core.camera.vo.request.EventExamineVo;
import com.fivefu.core.camera.vo.request.WorkorderVo;
import com.fivefu.core.camera.vo.response.MonitorWorkerBean;
import com.fivefu.core.module.auth.utils.SecurityUtil;
import com.fivefu.core.skyeye.event.utils.VerifyDataUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 场景工单表 前端控制器
 * </p>
 *
 * @author
 * @since 2022-09-19 19:38:06
 */
@Api(tags = "工单管理")
@RestController
@RequestMapping("/db_monitor_workorder")
public class DbMonitorWorkorderController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(DbSceneEventController.class);

    @Autowired
    private DbMonitorWorkorderService dbMonitorWorkorderService;
    @Autowired
    private SceneEventService sceneEventService;

    @ApiOperation(value = "工单列表",httpMethod = "POST",response = ResultInfo.class,notes = "工单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query",required = true, dataType = "int"),

            @ApiImplicitParam(name = "type", value = "工单类型(1:智能工单,2:人工工单)", paramType = "query",required = false, dataType = "int"),
            @ApiImplicitParam(name = "orderBigType", value = "问题大类", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "orderSmallType", value = "问题小类", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "orderAreaid", value = "所属城区", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "orderPushStatus", value = "状态", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraName", value = "探头名称", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cStime", value = "生成时间的开始(yyyy-MM-dd HH:mm:ss)", paramType = "query",required = false, dataType = "Date"),
            @ApiImplicitParam(name = "startEtime", value = "开始时间的结束(yyyy-MM-dd HH:mm:ss)", paramType = "query",required = false, dataType = "Date"),
    })
    @RequestMapping(path = "/queryMonitorWorkerList",method = RequestMethod.POST)
    public ResultInfo queryMonitorWorkerList(Integer page,Integer limit){
        try {
            if (page == null){
                return ResultInfo.renderError("页码不能为空");
            }
            if (limit == null){
                return ResultInfo.renderError("数量不能为空");
            }

            String type = StrUtils.isCheckNull(request.getParameter("type"));
            String orderBigType = StrUtils.isCheckNull(request.getParameter("orderBigType"));
            String orderSmallType = StrUtils.isCheckNull(request.getParameter("orderSmallType"));
            String orderAreaid = StrUtils.isCheckNull(request.getParameter("orderAreaid"));
            String orderPushStatus = StrUtils.isCheckNull(request.getParameter("orderPushStatus"));
            String cameraName = StrUtils.isCheckNull(request.getParameter("cameraName"));
            String cStime = StrUtils.isCheckNull(request.getParameter("cStime"));
            String cEtime = StrUtils.isCheckNull(request.getParameter("cEtime"));

            //验证时间格式
            if (StrUtils.isNotNull(cStime)){
                try {
                    VerifyDataUtils.verifyDateTime(cStime, "yyyy-MM-dd HH:mm:ss");
                    VerifyDataUtils.verifyDateTime(cEtime, "yyyy-MM-dd HH:mm:ss");
                }catch (Exception e){
                    return ResultInfo.renderError("生成时间格式错误");
                }
            }
            Page<MonitorWorkerBean> beanPage = dbMonitorWorkorderService.queryMonitorWorkerList(page,limit,type,orderBigType,orderSmallType,orderAreaid,orderPushStatus,cameraName,cStime,cEtime);
            return ResultInfo.renderSuccess(0,"查询工单列表成功",beanPage.getTotal(),beanPage.getRecords());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询工单列表失败");
    }

    @ApiOperation(value = "工单详情",httpMethod = "POST",response = ResultInfo.class,notes = "工单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "工单编号", paramType = "query",required = true, dataType = "Long")
    })
    @RequestMapping(path = "/queryMonitorWorkDetail",method = RequestMethod.POST)
    public ResultInfo queryMonitorWorkDetail(Long orderId){
        try {
            if (orderId == null){
                return ResultInfo.renderError("工单编号不能为空");
            }
            Map<String,Object> detail = dbMonitorWorkorderService.queryMonitorWorkDetail(orderId);
            return ResultInfo.renderSuccess(detail);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询工单列表失败");
    }

    @RequestMapping(path = "/createWorkorder",method = RequestMethod.POST)
    @ApiOperation(value = "生成工单",httpMethod = "POST",response = ResultInfo.class,notes = "生成工单")
    public ResultInfo createWorkorder(@ModelAttribute @Valid WorkorderVo workorderVo, @RequestAttribute String userid){
        try {
            logger.info("生成工单接收数据：" + JSON.toJSONString(request.getParameterMap()));
            String value = VerifyDataUtils.verifyRequiredColumn(workorderVo);
            if (StrUtils.isNotNull(value)){
                return ResultInfo.renderError(value + "不能为空");
            }
            Long orderId = sceneEventService.createWorkorder(workorderVo, userid);
            if (orderId != null){

                DbMonitorWorkorder order = dbMonitorWorkorderService.queryWorkorderById(orderId);
                Map<String,Object> map = new HashMap<>();
                map.put("id", order.getId());
                map.put("orderId", order.getOrderId());

                return ResultInfo.renderSuccess(200,"生成工单成功，工单编号为：" + order.getOrderId(),map);
            }else{
                return ResultInfo.renderSuccess("生成工单成功");
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("生成工单失败");
    }
}
